import getCookie from '@/utils/getCookie'
export default function ({ route, req, res, redirect }) {
  /* const token = '1'
  const whiteList = ['/index', '/new3']
  console.log(whiteList.includes(route.path))
  if (!token.length && !whiteList.includes(route.path)) {
    console.log('没有登录且不在白名单')
    return redirect('/form')
  } else {
    console.log('已经登录，或者在白名单')
  } */

  const isClient = process.client
  const isServer = process.server
  const redirectURL = '/'
  let token

  // 在服务端
  if (isServer) {
    const cookies = getCookie.getcookiesInServer(req)
    // 获取当前服务端cookie中是否含有token字段
    token = cookies.token ? cookies.token : ''
  }
  // 在客户端
  if (isClient) {
    token = getCookie.getcookiesInClient('token')
  }

  // 判断是否获取到token
  if (!token) {
    redirect(redirectURL)
  }
}
